define(function(require, exports){
	var $		= require('jquery');
	var pubjs	= require('pubjs');
	var view	= require('@base/view');
	var util	= require('util');

	//数字选择控件
	var CountSelector = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'target': pubjs.DEFAULT_POPUP_CONTAINER,
				'class': 'P-formCountSelector uk-form',
				'prefix': '',
				'suffix': '',
				'value': 0,
				'size': 2,
				'addCount': 0.5,
				'width': 150,
				'height': 100,
				'anchor': null
			});
			// 是否已经显示
			this.isShow = 0;
			this.$timeStamp = 0;

			this.Super('init', arguments);
			this.$orivalue = config.value;
		}
		,afterBuild:function(){
			var doms = this.doms = {},
				el = this.getContainer(),
				c = this.getConfig();

			$('<b class="prefix"/>').text(c.prefix).appendTo(el);
			this.input = doms.input = $('<input type="text" class="countInput"/>').appendTo(el);

			var btnCon = doms.btnCon = $('<div class="btnCon"/>').appendTo(el);
			doms.add = $('<span class="addIcon countSelectorCtr" data-action="add"><i class="triangle-bottom triangle-sm"/></span>').appendTo(btnCon);
			doms.sub = $('<span class="subIcon countSelectorCtr" data-action="sub"><i class="triangle-top triangle-sm"/></span>').appendTo(btnCon);
			$('<b class="suffix"/>').text(c.suffix).appendTo(el);
			doms.ctr =  $('<a href="javascript:void(0)" data-action="edit" class="edit"><i class="uk-icon-check-square font-success"/></a>').appendTo(el);
			this.setData(c.value);
			this.bindEvent();
		}
		,bindEvent:function(){
			var self = this;
			var el = this.getDOM();

			self.uiProxy(el,'.countSelectorCtr','click','eventClickCtr');
			self.uiProxy(el,'.edit','click','eventClickCtr');
			self.uiBind(self.input,'mousedown', self.focus);
			self.uiBind(self.input, 'keydown', function(ev){
				switch (ev.keyCode){
					// 回去保存
					case 13:
						self.save();
						break;
					//上下键可控制增减
					case 38:
						self.doAdd();
						break;
					case 40:
						self.doSub();
						break;
					default:
						return;
				}
				return false;
			});
			//阻止浏览器input默认拖动行为，chrome可在全选状态下拖动
			self.input.bind('dragstart', util.stopEvent);

			// 点击元素拦截隐藏
			self.uiBind(el,'mouseup tap',function (ev){
				self.$timeStamp = ev.timeStamp;
			});

			//不在focus状态，也能回车确定
			var id = self.$eventClassId = '.counter' + util.guid();
			self.uiBind(document, 'keydown' + id, function(ev){
				if (ev.keyCode == 13) {
					self.save();
					return false;
				}
			});
			self.uiBind(document, 'mouseup tap' + id, function (ev){
				if(ev.timeStamp !== self.$timeStamp){
					self.fire('cancelValue', function(){
						self.destroy();
					});
				}
			});
		}
		,beforeDestroy: function(){
			$(document).unbind('keydown' + this.$eventClassId)
				// .unbind('mouseup tap' + this.$eventClassId);
		}
		,eventClickCtr:function(evt,elm){
			var action = $(elm).attr("data-action");
			switch(action) {
				case "sub":
					this.doSub();
					break;
				case "add":
					this.doAdd();
					break;
				case "edit":
					this.save();
			}
			evt.preventDefault();
			return false;
		}
		,focus: function(){
			this.input.focus();
		}
		,select: function(){
			this.input.select();
		}
		,setData:function(data){
			var value = +data;
			var c = this.getConfig();
			if(!isNaN(value)){
				value = util.round0(value, c.size);
				this.input.val(value);
			}
			return this;
		}
		,getData:function(){
			return +this.input.val();
		}
		,doAdd:function(){
			var value = this.getData();
			var c = this.getConfig();
			this.setData(value + c.addCount);
		}
		,doSub:function(){
			var value = this.getData();
			var c = this.getConfig();
			this.setData(value - c.addCount);
		}
		//点击nike更新数据
		,save:function (){
			var data = this.getData();
			//广播事件
			this.fire('updateValue', data);
		}
		,show:function (param){

			if(!this.isShow){
				this.isShow = 1;
				var el = this.getDOM();
				var popOffset = util.popOffset();
				//修正位置
				$(el).css({
					'position': 'relative',
					'left': param.anchor.left - 70 + popOffset.left,
					'top': param.anchor.top - 14 + popOffset.top
				});
				this.setData(param.data);
				this.focus();
				this.select();
			}
		}
	})
	exports.base = CountSelector;

});